// pages/tibao/tibao.js
var common = require("../../utils/public.js") //引入public.js脚本
import WxValidate from "../../utils/wxValidate.js";
var api = require("../../utils/api");
var cf = require("../../config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagesList: [
      '../../images/icon_A.png'
    ],
    show: true,
    arrImgs:[],
    array: [],
    index: 0,
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    //获取分类
    api.demandClassList({
     
    }).then(res => {
      if (res.result) {
        let arr=[]

        that.setData({
          array: res.object,
          id: res.object[that.data.index].id
        })
        
      }
    })

    this.initValidate();
  },

  bindPickerChange: function (e) {
 
    this.setData({
      index: e.detail.value,
      id: this.data.array[e.detail.value].id
    })
  },
  // 删除图片
  deleteImages(e) {
    let that = this;
    let { imgBoard, imgStore, plaquePicUrl } = this.data;
    // 获取要删除的第几张图片的下标
    const idx = e.currentTarget.dataset.index;
    // splice  第一个参数是下表值  第二个参数是删除的数量

    if (idx === 0) {
      imgBoard.splice(0, 1);
      that.setData({
        imgBoard,
        boardShow: true
      })
    } else if (idx === 1) {
      imgStore.splice(0, 1);
      that.setData({
        imgStore,
        storeShow: true
      })
    } else if (idx === 2) {
      plaquePicUrl.splice(0, 1);
      that.setData({
        plaquePicUrl,
        plaqueBoard: true
      })
    }
  },
  // 提示信息
  initValidate() {
    let rules = {
      name: {
        required: true
      },
      // class1: {
      //   required: true,
      // },
      specs: {
        required: true,
   
      },
      monthNum: {
        required: true,
    
      },
      addr: {
        required: true,
    
      },
      mess: {
        required: true,

      }
    }
    let message = {
      name: {
        required: "请输入商品名称",
      },
      class1: {
        required: "请输入商品分类",
    
      },
      specs: {
        required: "输入商品规格",
   
      },
      monthNum: {
        required: "请输入月购买量",
      
      },
      addr: {
        required: "请输入产地",
      
      },
      mess: {
        required: "请输入品种、产地、口味等信息",

      }
    }
    this.WxValidate = new WxValidate(rules, message)
  },

  //上传图片
  addImg: function () {
    let that = this
    wx.chooseImage({
      count: 3,
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        if (res.errMsg == "chooseImage:ok") {
          that.setData({
            imagesList: res.tempFilePaths,
            show: false,
            arrImgs:[],
          })
          //调用上传图片 url 数组图片
          that.uploadFiles(`${cf.url}/admin/ossUploading/image`, res.tempFilePaths)
        }
      }
    })
  },

  //图片上传服务器
  uploadFiles: function (url, arrImg) {
    let that = this;
    let arrImgs = that.data.arrImgs
    let num = 0
    //遍历数组每张图片
    arrImg.forEach(function (v) {
      wx.uploadFile({
        url: url,
        filePath: v,
        name: 'file',
        formData: {
          path: 'submitNeed'
        },
        success(res) {

          res = JSON.parse(res.data)

          if (res.result) {
            arrImgs: arrImgs.push({
              url: res.data
            })
          } else {
            wx.showToast({
              title: '上传失败',
              icon: "none",
            })
          }
        }
      })
    })
    that.setData({
      arrImgs: arrImgs
    })
  },

  // 失败
  showModal(err) {
    wx.showModal({
      content: err.msg,
      showCancel: false
    })
  },

  formSubmit:function(e){
    console.log(e)
    let that = this

    if (!this.WxValidate.checkForm(e)) {
      let err = this.WxValidate.errorList[0]
      this.showModal(err);
      return false;
    }

    wx.showLoading({
      title: '加载中...'
    })

    api.toSelect({
      cityId: wx.getStorageSync("cityId"),
      createid: wx.getStorageSync("openId"),
      goodsName: e.detail.value.name,//名称
      goodsType: that.data.id,//分类id
      goodsSpec: e.detail.value.specs,//规格
      monthDemand: e.detail.value.monthNum,//月需求量
      prodArea: e.detail.value.addr,//产地
      remarks: e.detail.value.mess,//备注
      submitNeedsPicEntities: that.data.arrImgs//图片
    }).then(res => {
      if (res.result) {
        wx.navigateBack({
          data: 1
        })
      }
    })
    

  }
  
})